/**
 * jims
 */
package com.jims.master.modules.physical.service;

import com.google.common.collect.Lists;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.common.utils.PrintPdfUtils;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.impl.BaseDto;
import com.jims.master.modules.doctor.dao.ClinicMasterDao;
import com.jims.master.modules.doctor.dao.PatMasterIndexDao;
import com.jims.master.modules.doctor.entity.ClinicMaster;
import com.jims.master.modules.doctor.entity.PatMasterIndex;
import com.jims.master.modules.physical.dao.TInformationDao;
import com.jims.master.modules.physical.dao.TResultGroupDao;
import com.jims.master.modules.physical.entity.TInformation;
import com.jims.master.modules.physical.entity.TResultGroup;
import com.jims.master.modules.sys.utils.DictUtils;
import com.jims.master.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

;

/**
 * t_informationService
 * @author 系统管理员
 * @version 2017-06-14
 */
@Service
@Transactional(readOnly = true)
public class TInformationService extends CrudService<TInformationDao, TInformation> {

	@Autowired
	private PatMasterIndexDao patMasterIndexDao;
	@Autowired
	private ClinicMasterDao clinicMasterDao;
	@Autowired
	private TResultGroupDao tResultGroupDao;


	/**
	 * 根据病人Id查询体检信息
	 * @param clinicId
	 * @return
	 * @update_by CTQ AT 2017-06-28 09:54:32
	 */
	public TInformation findTInfoByPatId(String clinicId) {
		return dao.findTInfoByPatId(clinicId);
	}

	/**
	 * 打印导检单
	 * @param tInformation
	 * @author CTQ
	 * @return
	 */
	public String print(TInformation tInformation){
		String printPath="";
		Map map = new HashMap();
		BaseDto baseDto = new BaseDto();
		/**查询体检人员基本信息**/
		if (StringUtils.isNotBlank(tInformation.getPatientId())){
			PatMasterIndex pmi = patMasterIndexDao.get(tInformation.getPatientId());
			baseDto.put("name",pmi.getName());
			baseDto.put("sex", DictUtils.getDictValue(pmi.getSex(),"SEX_DICT",""));
			baseDto.put("age", pmi.getAge());
			baseDto.put("nation", DictUtils.getDictValue(pmi.getNation(),"NATION_DICT",""));
			baseDto.put("id_card", pmi.getIdNo());
			baseDto.put("phone", pmi.getPhone());
			baseDto.put("identity", DictUtils.getDictValue(pmi.getIdentity(),"PROFESSION_DICT",""));
			baseDto.put("address",pmi.getAddress());
			baseDto.put("marriage",DictUtils.getDictValue(pmi.getMarriage(),"MARRIAGE_DICT",""));
			map.putAll(baseDto);
		}
		/**查询体检人员体检信息**/
		List<BaseDto> list = Lists.newArrayList();
		if (StringUtils.isNotBlank(tInformation.getClinicId())){
			tInformation = dao.findTInfoByPatId(tInformation.getClinicId());
			ClinicMaster cm = clinicMasterDao.getById(tInformation.getClinicId());
			baseDto.put("healthexam_date",tInformation.getHealthexamDate());
			baseDto.put("clinic_no",cm.getClinicNo());
			baseDto.put("team",tInformation.getTeam());
			baseDto.put("team_group",tInformation.getTeamGroup());
			TResultGroup trg = new TResultGroup();
			trg.setClinicId(tInformation.getClinicId());
			trg.updateOrgId();
			list =tResultGroupDao.findGroupList(trg);
			map.putAll(baseDto);
			map.put("lists",list);
		}
		printPath = PrintPdfUtils.previewPdf(UserUtils.getUser().getId(), "his/phyExam/guideSheet.jasper", map, list, "guideSheet");

		return printPath;
	}
}